package top.hjcwzx.wrench.limiter.strategy.key;

import top.hjcwzx.wrench.limiter.enums.KeyStrategyEnum;

import javax.annotation.Resource;
import java.lang.reflect.Method;

//key执行器
public class RateLimitKeyExecutor {

    @Resource
    public RateLimiterKeyStrategyContext context;

    /**
     * 根据策略获取key
     * @param keyExpr key表达式
     * @param strategy 策略
     * @param method 方法
     * @param args 参数
     * @param target 目标
     * @return key
     */
    public String getKey(String keyExpr,KeyStrategyEnum strategy,
                         Method method, Object[] args, Object target) {
        RateLimiterKeyStrategy keyStrategy = context.getKeyStrategy(strategy);
        return keyStrategy.generateKey(method, args, target, keyExpr);
    }

}
